package com.testing.login;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Classname ${NAME}
 * @Description 类型说明
 * @Date 2020/12/26 21:08
 * @Created by 特斯汀Roy
 */
public class Logout extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        //获取会话session
        HttpSession session = request.getSession();
        //判断loginName是否为空，如果不为空说明登录过

        if(session.getAttribute("loginName")!=null){
            String loginName = session.getAttribute("loginName").toString();
            response.getWriter().append("{\"status\":1,\"msg\":\"" + "注销"+loginName+"用户成功" + "\"}");
        }
        else{
            //说明系统当前没有登录过
            response.getWriter().append("{\"status\":1,\"msg\":\"" + "没有登录过，不需要注销" + "\"}");
        }
        //将session置为失效状态。
        session.invalidate();

    }
}
